package com.fancy.learncenter.ui.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.utils.ImageUtil;
import com.fancy.learncenter.utils.WXShareUtil;

import org.json.JSONException;

/**
 * Created by Hyy on 2017/2/14.
 */

public class SharePopupWindow extends BasePopupWindow {
    View popopView;

    View locationView;

    Context mContext;
    LinearLayout rootLl;
    private CardView ll_popup;
    private AnimatorSet animSet;
    private ObjectAnimator animatorAlpha;
    private ObjectAnimator animatorScalex;


    ImageView colse;
    ImageView wx;
    ImageView wxFriend;

    public SharePopupWindow(Activity mContext, View locationView) {
        super(mContext);
        this.mContext = mContext;
        this.locationView = locationView;
        popopView = LayoutInflater.from(MyApplication.context).inflate(R.layout.popupwindow_share, (ViewGroup) locationView, false);
        ll_popup = (CardView) popopView.findViewById(R.id.ll_popup);

        colse = (ImageView) popopView.findViewById(R.id.colse);
        wx = (ImageView) popopView.findViewById(R.id.wx);
        wxFriend = (ImageView) popopView.findViewById(R.id.wx_friend);
        initPopupWionw();

    }

    private void initPopupWionw() {

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        setOutsideTouchable(true);

        setContentView(popopView);
        popopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ll_popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        colse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareCallBack.wxCallBack();
//                WXShareUtil.getInstance(mContext).sharePicBitmap(ImageUtil.loadBitmapFromView(ll_popup),false);

            }
        });
        wxFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareCallBack.wxFriendCallBack();
            }
        });
    }


    @Override
    public void showPopupWindow() {
        super.showPopupWindow();

        animatorAlpha = ObjectAnimator.ofFloat(ll_popup, "alpha", 0, 0.8f, 1f);
        int animateHeight = ll_popup.getHeight();
        if (animateHeight <= 0) {
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            ll_popup.measure(w, h);
            animateHeight = ll_popup.getMeasuredHeight();
        }

        animatorScalex = ObjectAnimator.ofFloat(ll_popup, "translationY", -animateHeight, 200, 0);


        animSet = new AnimatorSet();
        animSet.play(animatorAlpha).with(animatorScalex);
        animSet.setDuration(1500);
        animSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animSet.start();

        showAtLocation(locationView, Gravity.CENTER, 0, 0);
    }

    private ShareCallBack shareCallBack;

    public ShareCallBack getShareCallBack() {
        return shareCallBack;
    }

    public void setShareCallBack(ShareCallBack shareCallBack) {
        this.shareCallBack = shareCallBack;
    }

    public interface ShareCallBack {
        // 分享到微信好友
        void wxCallBack();

        //分享到微信朋友圈
        void wxFriendCallBack();
    }
}
